<?php
namespace app\index\controller;
use think\Request;
use think\Db;
/**
* 
*/
class Weather extends \think\Controller
{
	
	public function getClimateInfo(){

    $proarr = ['北京市', '上海市', '天津市', '重庆市']; 
		$request = Request::instance();
        if($request->isPost()){
        	$data = $request->except(['name']);
          $equnique = $data['equnique'];
          $map = [
              'ID' =>  ['=', $equnique]
          ];
          $resdata = Db::table('published_seismic_database')->field('eqtname, province, abbreviation')->where($map)->select();
          if(!$resdata) return $this->packData(null);
          $res = $resdata[0];
          $pro = trim($res['province']);
          $name = trim($res['abbreviation']);
          if(in_array($pro, $proarr)) {
            $city = $pro;
          }else {
            $city = trim($res['eqtname']);
          }
        }
        //var_dump($city);
       	$url='http://wthrcdn.etouch.cn/WeatherApi?city='.$city;
        // var_dump($url);
       	$ch = curl_init();
          $timeout = 5;
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch,CURLOPT_ENCODING ,'gzip');
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $file_contents = curl_exec($ch);
          curl_close($ch);
          $climate = Db::table('published_seismic_database')->field('climate')->where($map)->select();
          $clidescription = $climate[0];
          $clidata=json_decode(json_encode(simplexml_load_string($file_contents, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
          // var_dump($clidata);
          $data = [
            'clidescription' => $clidescription,
            'clidata' =>  $clidata,
            'name' => $name
          ];
          $data = json_encode($data);
         return $data;
      
      //  $url = 'http://wthrcdn.etouch.cn/weather_mini?city=北京';
      //   $html = file_get_contents($url);
      // // echo urldecode($html);
      //   echo gzdecode($html);
     
		 
     }
}
?>